package com.google.code.whitetask.old;

/**
 * <p>
 * This class is a Task Decorator to retry the execution of a task when the task
 * is failed.
 * </p>
 * 
 * @author minslovey
 */

public class RetryDecorator extends AbstractFluentTask {
  private Task task;
  Times times = new Times(2);

  public RetryDecorator(Task task) {
    this.task = task;
  }

  public TaskResultType execute(TaskContext context) {
    for (int i = 0; i <= times.getTimes(); i++) {
      TaskResultType result = task.execute(context);

      if (result == TaskResultType.SUCCESS) {
        return result;
      }
    }

    return TaskResultType.FAILURE;
  }

  public RetryDecorator times(Times times) {
    this.times = times;
    return this;
  }   
}
